﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Dude.MLS;

namespace Dude.Data.Meta
{
    public class GenericSQLCreateScriptBuider: SQLCreateScriptBuiderBase
    {
        public TextWriter Output { get; set; }

        public GenericSQLCreateScriptBuider()
        {
            Output = null;
        }

        public GenericSQLCreateScriptBuider(TextWriter output)
        {
            Output = output;
        }

        protected virtual void WriteCommentBlock(string comment)
        {
            Output.Write("/* ");
            Output.WriteLine(comment.Trim().Replace("*/", "* /").Replace("\n", "\n * "));
            Output.WriteLine(" */");
        }

        protected override object StartDatabaseInfo(DatabaseInfo databaseInfo)
        {
            WriteCommentBlock("Database name: {0}\nUsing connection: {1}\nClient display name:{2}\nDescription:\n{3}".GetText(databaseInfo.DatabaseName, databaseInfo.ConnectionName, databaseInfo.DisplayName.ToString(), databaseInfo.Description.ToString()));
            Output.WriteLine("CREATE DATABASE `{0}`; ", databaseInfo.DatabaseName);
            Output.WriteLine("USING DATABASE `{0}`; ", databaseInfo.DatabaseName);
            Output.WriteLine();
            return null;
        }

        private bool firstfield = false;

        protected override object StartTableInfo(TableInfo tableInfo)
        {
            Output.WriteLine();
            WriteCommentBlock("Table name: {0}\nClient display name:{1}\nDescription:\n{2}".GetText(tableInfo.TableName, tableInfo.DisplayName.ToString(), tableInfo.Description.ToString()));
            Output.WriteLine("CREATE TABLE `{0}`\n(", tableInfo.DBTableName);
            firstfield = true;
            return null;
        }

        protected override object StartDataFieldInfo(DataFieldInfo fieldInfo)
        {
            if (firstfield)
            {
                firstfield = false;
            }
            else
            {
                Output.WriteLine(",");
            }

            Output.Write("  `{0}` {1}", fieldInfo.DBFieldName, fieldInfo.DBTypeName);

            return null;
        }

        protected override void StopTableInfo(TableInfo tableInfo, object data)
        {
            // write key definition here...

            Output.WriteLine("");
            Output.WriteLine(");");

            // write foreign keys to buffer here and write it before database definition closes...
        }

        protected override object StartDatabaseConnectionInfo(DatabaseConnectionInfo databaseConnectionInfo)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("Database connection name: {0}\n".GetText(databaseConnectionInfo.ConnectionName));
            foreach(string alias in databaseConnectionInfo.Aliases)
            {
                sb.Append("Connection alias: {0}\n".GetText(alias));
            }
            sb.Append("Connection description: {0}\n".GetText(databaseConnectionInfo.Description.ToString()));
            sb.Append("Database connection string: {0}\n".GetText(databaseConnectionInfo.ConnectionString));
            WriteCommentBlock(sb.ToString());
            return null;
        }
    }
}
